package com.hzc.springchan.util;

import com.hzc.springchan.constant.System;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * 日志打印工具类
 * @author chen990627
 * @date 2022/09/09
 */
public class LogUtils {

    private LogUtils() {
    }

    private static Map<Class, Logger> container;

    private static Logger ERROR_LOG = LoggerFactory.getLogger(System.LogLevel.ERROR);
    private static Logger INTER_LOG = LoggerFactory.getLogger(System.LogLevel.INTER);

    static {
        container = new HashMap<>(64);
    }

    public static void info(Object object, String pattern, Object... params) {
        getLogger(object.getClass()).info(pattern, params);
    }

    public static void inter(String pattern, Object... params) {
        INTER_LOG.info(pattern, params);
    }

    public static void error(String pattern, Object... params) {
        ERROR_LOG.error(pattern, params);
    }

    private static <T> Logger getLogger(T instance) {
        Class clazz = instance.getClass();
        Logger logger = container.get(clazz);
        if (logger == null) {
            synchronized (LogUtils.class) {
                if (logger == null) {
                    container.put(clazz, logger = newLogger(clazz));
                }
            }
        }
        return logger;
    }

    private static Logger newLogger(Class clazz) {
        return LoggerFactory.getLogger(clazz);
    }

}
